<?php
namespace apiadmin\modules\v1\controllers\finance;
use common\models\StaticInstance;
use \Yii;
use apiadmin\modules\v1\controllers\CoreController;
use common\models\finance\StaticInstanceModel;
use common\models\finance\StaticInstanceOrderModel;
use common\utils\OutputExecl;
/*
	投资相关控制器
*/
class InvestController extends CoreController
{

	//投资列表
	public function actionInvset_list()
	{
		$params = array(
			'page'	=> $this->request('page',1),
			'limit'	=> $this->request('page_size'),
			'order'	=> 'id desc'
		);
		$whereArr = $this->formatWhere();
		$list = StaticInstance::investList($whereArr,$params);
		$pages = StaticInstance::$pages;

		//汇总
		$extend = [
			'pages'=>$pages,
		];

		$this->out('投资列表',$list,$extend);
	}

	private function formatWhere()
	{
		$whereArr = $where = $and = [];
		$search = json_decode($this->request('search'),1);

		if(isset($search['member_id']) && $search['member_id'])
			$where['member_id'] = $search['member_id'];

		if(isset($search['state']) && $search['state'])
			$where['state'] = $search['state'];		

		if(isset($search['type']) && $search['type'])
			$where['type'] = $search['type'];			

		$date = $search['date'];
		if($date && $date['0'] && $date['1'])
			$and[] = ['between', 'create_time', strtotime($date[0]),strtotime($date[1])];

		$whereArr['where'] = $where;
		$whereArr['and'] = $and;
		return $whereArr;
	}



	public function actionInvset_order()
	{
		$params = array(
			'page'	=> $this->request('page',1),
			'limit'	=> $this->request('page_size'),
			'order'	=> 'id desc'
		);
		$whereArr = $this->formatWhere();
		$list = StaticInstanceOrderModel::investOrderList($whereArr,$params);
		$pages = StaticInstanceOrderModel::$pages;

		//汇总
		$queryModel = StaticInstanceOrderModel::$queryModel;
		$res = $queryModel->select(['sum(total_num) as total','sum(in_num) as inNum'])->one();
		$total = isset($res['total'])?$res['total']:0;
		$inNum = isset($res['inNum'])?$res['inNum']:0;

		$extend = [
			'pages'=>$pages,
			'total'=>$total,
			'inNum'	=> $inNum,
			'type_name' => array('1'=>'挖矿','2'=>'增量'),
		];

		$this->out('投资订单列表',$list,$extend);
	}

}